package handler

import (
	"context"
	"srv/global"
	"srv/internal/model"
	__ "srv/proto"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedVideoSrvServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) ViesList(_ context.Context, in *__.VideoReq) (*__.VideoResp, error) {
	page := in.Page
	if page <= 0 {
		page = 1
	}

	pageSize := in.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	var vies []*model.Vies
	global.DB.Where("status = ?", in.Status).Offset(int(offset)).Limit(int(pageSize)).Find(&vies)
	var vs []*__.ViesList
	for _, vy := range vies {
		vs = append(vs, &__.ViesList{
			Video:  vy.Video,
			Status: vy.Status,
			Desc:   vy.Desc,
		})
	}
	return &__.VideoResp{
		List: vs,
	}, nil
}

// SayHello implements helloworld.GreeterServer
func (s *Server) UserList(_ context.Context, in *__.UserListReq) (*__.UserListResp, error) {

	var vies []*model.User
	global.DB.Find(&vies)
	var vs []*__.UserList
	for _, vy := range vies {
		vs = append(vs, &__.UserList{
			Mobile:   vy.Mobile,
			Username: vy.Username,
			Desc:     vy.Desc,
			Address:  vy.Address,
			Img:      vy.Img,
		})
	}
	return &__.UserListResp{
		List: vs,
	}, nil
}
